package dxn.nl.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import dxn.boot.common.entity.CsBasePageQuery;
import dxn.boot.common.entity.CsBasePageResult;
import dxn.nl.file.biz.dao.ExcelExportDao;
import dxn.nl.file.biz.po.FileExportApplyDetail;
import dxn.nl.file.biz.vo.CsExportVo;
import dxn.nl.file.service.CsExportService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CsExportServiceImpl implements CsExportService {

    @Resource
    private ExcelExportDao excelExportDao;

    @Override
    public CsBasePageResult<FileExportApplyDetail> pageVo(CsBasePageQuery<CsExportVo> pageQuery) {

        CsExportVo vo = pageQuery.getQuery();

        LambdaQueryWrapper<FileExportApplyDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(FileExportApplyDetail::getApplyTime, vo.getEndTime())
                .gt(FileExportApplyDetail::getApplyTime, vo.getStartTime());

        if(StringUtils.isNotBlank(vo.getOrgCode()))
        {
            lambdaQueryWrapper.eq(FileExportApplyDetail::getOrgCode,vo.getOrgCode());
        }

        if(StringUtils.isNotBlank(vo.getApplyUserCode()))
        {
            lambdaQueryWrapper.eq(FileExportApplyDetail::getApplyUserCode,vo.getApplyUserCode());
        }

        if(StringUtils.isNotBlank(vo.getModeCode()))
        {
            lambdaQueryWrapper.eq(FileExportApplyDetail::getModeCode,vo.getModeCode());
        }

        if(vo.getStatus() != null)
        {
            lambdaQueryWrapper.eq(FileExportApplyDetail::getStatus,vo.getStatus());
        }

        if(StringUtils.isNotBlank(vo.getFileName()))
        {
            lambdaQueryWrapper.like(FileExportApplyDetail::getFileName,"%"+vo.getFileName()+"%");
        }

        lambdaQueryWrapper.orderByDesc(FileExportApplyDetail::getApplyTime);

        IPage page = Page.of(pageQuery.getPageIndex(),pageQuery.getPageSize(),pageQuery.isCheckCount());
        page = excelExportDao.selectPage(page,lambdaQueryWrapper);

        CsBasePageResult<FileExportApplyDetail> pageResult = new CsBasePageResult<>();
        pageResult.setRows(page.getRecords());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    @Override
    public String applyExport(FileExportApplyDetail applyDetail) {

        applyDetail.setConsumerRefreshTime(LocalDateTime.now());

        int result = excelExportDao.insert(applyDetail);

        if(result >0)
        {
            return null;
        }

        return "导出申请失败";
    }

    @Override
    public String updateCanConsume(FileExportApplyDetail applyDetail) {
        int updateCount = excelExportDao.updateCanConsume(applyDetail);
        if(updateCount >0)
        {
            return null;
        }

        return "重新处理申请失败";
    }
}
